package com.study.controller;

import com.common.ResultObj;
import com.study.entity.Role;
import com.study.entity.User;
import com.study.entity.UserVo;
import com.study.service.RoleService;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    // 显示用户信息
    @RequestMapping(value = "/userManagement",method = RequestMethod.GET)
    public ModelAndView showUsers() {
        ModelAndView mv = new ModelAndView();
        // 获取所有用户信息及其具有的角色
        List<User> userList = userService.selectAllUser_role();
        mv.addObject("userList", userList);
        mv.setViewName("user-list");
        return mv;
    }

    // 新建用户
    @RequestMapping(value = "/addUser",method = RequestMethod.GET)
    public ModelAndView addUser() {
        ModelAndView mv = new ModelAndView();
        // 获取角色信息
        List<Role> roleList = roleService.selectAllRole();
        mv.addObject("roleList", roleList);
        mv.setViewName("user-add");
        return mv;
    }

    // 新增用户的同时给用户增加权限
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(User user, String[] roleId) {
        // 新增用户
        if (userService.addUser(user) == 1) {
            User userByName = userService.findUserInfoByName(user.getLoginName());
            Integer userId = userByName.getUserId();
            userService.saveUserPrivilege(userId,roleId);
        }
        return "redirect:/user/userManagement";
    }

    // 删除用户
    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    public String deleteUser(Integer userId) {
        userService.deleteUser(userId);
        return "redirect:/user/userManagement";
    }

    // 分配权限
    @RequestMapping(value = "/permissionAssignment/{userId}",method = RequestMethod.GET)
    public ModelAndView permissionAssignment(@PathVariable Integer userId) {
        ModelAndView mv = new ModelAndView();
        User user = userService.selectUser(userId);
        List<Role> roleList = roleService.selectAllRole();
        mv.addObject("user",user);
        mv.addObject("roleList",roleList);
        mv.setViewName("user-update");
        return mv;
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public String updateUser(User user, String[] roleId) {
        // 修改用户信息
        User userByName = userService.findUserInfoByName(user.getLoginName());
        userByName.setUserName(user.getUserName());
        userService.modifyUser(userByName);

        // 权限分配
        roleService.saveAssignedRole(userByName.getUserId(),roleId);
        return "redirect:/user/userManagement";
    }

    /**
     * 修改密码
     * @param userVo
     * @param session
     * @return
     */
    @RequestMapping("changePwd")
    @ResponseBody
    public ResultObj changePwd(UserVo userVo, HttpSession session) {
        try {
            //验证两次输入的密码是否一致
            if (!userVo.getNewPwd().equals(userVo.getConfirmPwd())) {
                return ResultObj.CHANGEPWD_COMFIRMPWD_ERROR;
            }
            //验证旧密码输入是否正确
            User user = (User) session.getAttribute("user");
            String truePwd = user.getLoginPwd();// 登录密码
            System.out.println("正确密码:" + truePwd);
            System.out.println("输入的密码:" + userVo.getOldPwd());
            if (!truePwd.equals(userVo.getOldPwd())) {
                return ResultObj.CHANGEPWD_OLDPWD_ERROR;
            }

            String newPwd = userVo.getNewPwd();
            user.setLoginPwd(newPwd);
            userService.modifyUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.CHANGEPWD_ERROR;
        }
        return ResultObj.CHANGEPWD_SUCCESS;
    }
}
